package com.kyrioscraft.cloud.security.securitymoblie;

import com.kyrioscraft.cloud.security.core.AbstractChannelSecurityConfig;
import com.kyrioscraft.cloud.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import com.kyrioscraft.cloud.security.core.properties.SecurityConstants;
import com.kyrioscraft.cloud.security.core.properties.SecurityProperties;
import com.kyrioscraft.cloud.security.core.validate.code.ValidateCodeSecurityConfig;
import com.kyrioscraft.cloud.security.securitymoblie.authentication.social.OpenIdAuthenticationSecurityConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.social.security.SpringSocialConfigurer;

@Configuration
@Slf4j
public class AppSecurityConfig extends AbstractChannelSecurityConfig {
    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private ValidateCodeSecurityConfig validateCodeSecurityConfig;

    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Autowired
    private OpenIdAuthenticationSecurityConfig openIdAuthenticationSecurityConfig;

    @Autowired
    private SpringSocialConfigurer mySocialSecurityConfig;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        log.info("loading security config.............");

        http.formLogin()
                .loginPage(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
                .loginProcessingUrl(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_FORM)
                .successHandler(myAuthenticationSuccessHandler)
                .failureHandler(myAuthenticationFailureHandler);


        http
                .apply(validateCodeSecurityConfig)
                .and()
                .apply(smsCodeAuthenticationSecurityConfig)
                .and()
                .apply(openIdAuthenticationSecurityConfig)
                .and()
                .apply(mySocialSecurityConfig)
                .and()
//                .userDetailsService(userDetailsService)
                .authorizeRequests()
                .antMatchers(
                        SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
                        SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
                        securityProperties.getBrowser().getLoginPage(),
                        SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX+"/*",
                        securityProperties.getBrowser().getSignUpUrl(),
//                        securityProperties.getBrowser().getSession().getSessionInvalidUrl()+".json",
//                        securityProperties.getBrowser().getSession().getSessionInvalidUrl()+".html",
                        "/user/regist",
                        "/demo-signIn.html",
                        "/authentication/openId",
                        "/social/signUp").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf().disable();
    }
}
